/*
**************************************************************************************
*Myriad Engine                                                                       *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
package leola.types;

import java.util.ArrayList;
import java.util.List;

import leola.backend.interpreter.Scope;


/**
 * An array
 * 
 * @author Tony
 *
 */
public class LeoArray extends LeoScopedObject {

	/**
	 * Array
	 */
	private List<LeoObject> array;
	
	/**
	 */
	public LeoArray() {
		this(new ArrayList<LeoObject>());
	}

	/**
	 * @param array
	 */
	public LeoArray(List<LeoObject> array) {
		super(LeoType.ARRAY, new Scope(true));
		
		this.array = array;
		
		addMethod("add"		, LeoObject.class);
		addMethod("remove"	, LeoObject.class);
		addMethod("get"		, int.class);
		addMethod("slice"	, int.class, int.class);
		addMethod("tail"	, int.class);
		addMethod("size"			   );
		
		// stack operations
		addMethod("push"	, LeoObject.class);
		addMethod("pop"		                 );
		addMethod("peek"	                 );
		addMethod("empty"	                 );
		
		
		// functional operations
		addMethod("rest"	                 );
		addMethod("first"	                 );
	}
	
	/* (non-Javadoc)
	 * @see leola.types.LeoObject#toString()
	 */
	@Override
	public String toString() {
		return this.array.toString();
	}
	
	/**
	 * Adds an object
	 * @param obj
	 */
	public void add(LeoObject obj) {
		this.array.add(obj);
	}
	public void push(LeoObject obj) {
		this.array.add(obj);
	}
	
	/**
	 * Removes an object.
	 * @param obj
	 */
	public boolean remove(LeoObject obj) {
		return this.array.remove(obj);
	}
	
	/**
	 * @return
	 */
	public LeoObject pop() {
		return this.array.remove(this.array.size()-1);
	}
	
	/**
	 * @return
	 */
	public LeoObject peek() {
		return this.array.get(this.array.size()-1);
	}
	
	/**
	 * Gets an element
	 * @param i
	 * @return
	 */
	public LeoObject get(int i) {
		return this.array.get(i);
	}
	
	/**
	 * @return the size of the array
	 */
	public int size() {
		return this.array.size();
	}
	
	/**
	 * @return
	 */
	public boolean empty() {
		return this.array.isEmpty();
	}
	
	/**
	 * Sets an element
	 * @param index
	 * @param obj
	 */
	public void set(int index, LeoObject obj) {
		this.array.set(index, obj);
	}
	
	/**
	 * Sets the element at the provided index
	 * @param index
	 * @param obj
	 */
	public void set(LeoNumber index, LeoObject obj) {
		this.array.set( (int)index.getNumber(), obj);
	}
	
	/**
	 * gets a sublist 
	 * @param start
	 * @param end
	 * @return
	 */
	public LeoArray slice(int start, int end) {
		return new LeoArray(this.array.subList(start, end));
	}
	
	/**
	 * gets a sublist 
	 * @param start
	 * @param end
	 * @return
	 */
	public LeoArray tail(int start) {
		return new LeoArray(this.array.subList(start, this.array.size()));
	}
	
	/**
	 * @return the first element
	 */
	public LeoObject first() {
		return this.array.get(0);
	}
	
	/**
	 * Truncates the first element and returns the rest of the array.
	 * @return
	 */
	public LeoObject rest() {
		if (this.array.size() < 2 ) return LeoNull.LEONULL;
		return new LeoArray(this.array.subList(1, this.array.size()));
	}
	
	/**
	 * @return the array
	 */
	public List<LeoObject> getArray() {
		return array;
	}
	
	/* (non-Javadoc)
	 * @see leola.types.LeoObject#clone()
	 */
	@Override
	public LeoObject clone() {
		return new LeoArray(new ArrayList<LeoObject>(this.array));
	}

	/* (non-Javadoc)
	 * @see leola.types.LeoObject#eq(leola.types.LeoObject)
	 */
	@Override
	public boolean eq(LeoObject other) {
		return this.array.equals(other);
	}

	/* (non-Javadoc)
	 * @see leola.types.LeoObject#getValue()
	 */
	@Override
	public Object getValue() {
		return this; /*this.array;*/
	}

	/* (non-Javadoc)
	 * @see leola.types.LeoObject#gt(leola.types.LeoObject)
	 */
	@Override
	public boolean gt(LeoObject other) {
		return false;
	}

	/* (non-Javadoc)
	 * @see leola.types.LeoObject#lt(leola.types.LeoObject)
	 */
	@Override
	public boolean lt(LeoObject other) {
		return false;
	}

}
